package utils

import (
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

// GetNacosClientConfig 定义客户端配置
func GetNacosClientConfig() constant.ClientConfig {
	clientConfig := constant.ClientConfig{
		//NamespaceId: GlobalEnv.Nacos.Client_configs.Namespace_id,
		NamespaceId: "f509d58f-275b-4d60-b9f7-37369a7a608c",
		//RegionId: GlobalEnv.Nacos.Client_configs.Region_id,
		RegionId: "go-oss",
		//TimeoutMs: GlobalEnv.Nacos.Client_configs.Timeout_ms,
		TimeoutMs: 3000,
		//NotLoadCacheAtStart: GlobalEnv.Nacos.Client_configs.Not_load_cache_at_start,
		NotLoadCacheAtStart: true,
		//LogDir: GlobalEnv.Nacos.Client_configs.Log_dir,
		LogDir: "/tmp/nacos/log",
		//CacheDir: GlobalEnv.Nacos.Client_configs.Cache_dir,
		CacheDir: "/tmp/nacos/cache",
		//LogLevel: GlobalEnv.Nacos.Client_configs.Log_level,
		LogLevel: "debug",
	}
	return clientConfig
}

// GetNacosClientConfig 至少一个ServerConfig
func GetNacosServerConfigs() []constant.ServerConfig {
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "nacos.topwhere.cn",
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},

		//{
		//	IpAddr:      "console2.nacos.io",
		//	ContextPath: "/nacos",
		//	Port:        80,
		//	Scheme:      "http",
		//},
	}
	return serverConfigs
}

func GetAddrs() []string {
	addrs := make([]string, 1)
	addrs[0] = GlobalEnv.Nacos.Http + ":" + GlobalEnv.Nacos.Port
	return addrs
}

// GetNacosClient 创建链接
func GetNacosClient() vo.NacosClientParam {
	var clientConfig = GetNacosClientConfig()
	serverConfigs := GetNacosServerConfigs()
	NacosClientvo := vo.NacosClientParam{
		ClientConfig:  &clientConfig,
		ServerConfigs: serverConfigs,
	}
	return NacosClientvo
}

// GetNacosNamingClient 服务发现创建命名客户端
func GetNacosNamingClient() naming_client.INamingClient {
	GetNacosClient := GetNacosClient()
	namingClient, _ := clients.NewNamingClient(GetNacosClient)
	return namingClient
}

// GetNacosConfigClient 动态配置创建配置客户端
func GetNacosConfigClient() config_client.IConfigClient {
	GetNacosClient := GetNacosClient()
	configClient, _ := clients.NewConfigClient(GetNacosClient)
	return configClient
}
